/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2024 All Rights Reserved.
 */
package com.alipay.demo.util;

import com.alipay.api.*;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jishupei.jsp
 * @version : AlipaySdkUtil, v0.1 2024年03月04日 3:18 下午 jishupei.jsp Exp $
 */
@Component
@ConfigurationProperties(prefix = "alipay")
public class AlipaySdkUtil {
    private AlipayClient alipayClient;

    private String appId;

    // 应用私钥
    private String privateKey;

    // 支付宝公钥（alipay.alipaypublickey）
    private String alipayPublicKey;

    // 商户ID（alipay.pid）
    private String pid;

    // 是否使用OpenId（alipay.use_open_id）
    private boolean useOpenId;

    // 使用OpenId时必填（alipay.open_id）
    private String openId;

    // 用户ID，和OpenId二选一（alipay.user_id）
    private String userId;

    // 是否是服务商（alipay.is_isv）
    private boolean isIsv;

    // 服务商必填（alipay.app_auth_token）
    private String appAuthToken;

    @PostConstruct
    public void init() throws AlipayApiException {
        // 初始化v2 SDK
        this.alipayClient = new DefaultAlipayClient(getAlipayConfig());
    }

    public <T extends AlipayResponse> T execute(AlipayRequest<T> request) throws AlipayApiException {
        if (isIsv) {
            return alipayClient.execute(request, null, appAuthToken);
        }
        return alipayClient.execute(request);
    }

    private AlipayConfig getAlipayConfig() {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId(appId);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setFormat("json");
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        Map<String, String> headers = new HashMap<>();
        headers.put("alipay-sdk-demo", "app-item-0.0.1");
        alipayConfig.setCustomHeaders(headers);
        return alipayConfig;
    }

    /**
     * Getter method for property <tt>pid</tt>.
     *
     * @return property value of pid
     */
    public String getPid() {
        return pid;
    }

    /**
     * Setter method for property <tt>pid</tt>.
     *
     * @param pid value to be assigned to property pid
     */
    public void setPid(String pid) {
        this.pid = pid;
    }

    /**
     * Getter method for property <tt>appId</tt>.
     *
     * @return property value of appId
     */
    public String getAppId() {
        return appId;
    }

    /**
     * Setter method for property <tt>appId</tt>.
     *
     * @param appId value to be assigned to property appId
     */
    public void setAppId(String appId) {
        this.appId = appId;
    }

    /**
     * Getter method for property <tt>openId</tt>.
     *
     * @return property value of openId
     */
    public String getOpenId() {
        return openId;
    }

    /**
     * Setter method for property <tt>openId</tt>.
     *
     * @param openId value to be assigned to property openId
     */
    public void setOpenId(String openId) {
        this.openId = openId;
    }

    /**
     * Getter method for property <tt>userId</tt>.
     *
     * @return property value of userId
     */
    public String getUserId() {
        return userId;
    }

    /**
     * Setter method for property <tt>userId</tt>.
     *
     * @param userId value to be assigned to property userId
     */
    public void setUserId(String userId) {
        this.userId = userId;
    }

    /**
     * Getter method for property <tt>useOpenId</tt>.
     *
     * @return property value of useOpenId
     */
    public boolean isUseOpenId() {
        return useOpenId;
    }

    /**
     * Setter method for property <tt>useOpenId</tt>.
     *
     * @param useOpenId value to be assigned to property useOpenId
     */
    public void setUseOpenId(boolean useOpenId) {
        this.useOpenId = useOpenId;
    }

    /**
     * Getter method for property <tt>isIsv</tt>.
     *
     * @return property value of isIsv
     */
    public boolean isv() {
        return isIsv;
    }

    /**
     * Setter method for property <tt>isIsv</tt>.
     *
     * @param isv value to be assigned to property isIsv
     */
    public void setIsv(boolean isv) {
        isIsv = isv;
    }

    /**
     * Getter method for property <tt>appAuthToken</tt>.
     *
     * @return property value of appAuthToken
     */
    public String getAppAuthToken() {
        return appAuthToken;
    }

    /**
     * Setter method for property <tt>appAuthToken</tt>.
     *
     * @param appAuthToken value to be assigned to property appAuthToken
     */
    public void setAppAuthToken(String appAuthToken) {
        this.appAuthToken = appAuthToken;
    }

    /**
     * Getter method for property <tt>privateKey</tt>.
     *
     * @return property value of privateKey
     */
    public String getPrivateKey() {
        return privateKey;
    }

    /**
     * Setter method for property <tt>privateKey</tt>.
     *
     * @param privateKey value to be assigned to property privateKey
     */
    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
    }

    /**
     * Getter method for property <tt>alipayPublicKey</tt>.
     *
     * @return property value of alipayPublicKey
     */
    public String getAlipayPublicKey() {
        return alipayPublicKey;
    }

    /**
     * Setter method for property <tt>alipayPublicKey</tt>.
     *
     * @param alipayPublicKey value to be assigned to property alipayPublicKey
     */
    public void setAlipayPublicKey(String alipayPublicKey) {
        this.alipayPublicKey = alipayPublicKey;
    }
}
